<?php
class UserController extends Controller {

	public function indexAction() {

	}

	//check user login
	public function loginAction() {
		session_start();
		//get params
		$username = $this->get_params('username');
		$password = $this->get_params('password');

		if(!empty($username) && !empty($password)){
			// 实例化模块
			$user_model = $this->model('user');
			$data = $user_model->from('user')->where('username=?', $username)->where('password=?', $password)->select();

			//用户不存在
			if(empty($data)){
				$url = $this->create_url('user/register');
				$this->show_message('用户名或者密码错误',$url,1 );
			}

			//用户存在
			$_SESSION['login'] = true;
			$_SESSION['userid'] = $data[0]['id'];
			
			//去腾讯吧
			$url = $this->create_url('tengxun/index');
			$this->show_message('登录成功，进入页面',$url,1 );
		}

		// $this->assign();
		$this->display();
	}

	public function registerAction() {
		$submit = $this->get_params('submit');
		if(!empty($submit)){
			// 实例化模块
			$user_model = $this->model('user');
			$userdata = array();
			$userdata['username'] = $this->get_params('username');
			$userdata['password'] = $this->get_params('password');
			$userdata['regname'] = $this->get_params('regname');
			$userdata['regrealname'] = $this->get_params('regrealname');
			$userdata['regbirthday'] = $this->get_params('regbirthday');
			$userdata['regemail'] = $this->get_params('regemail');
			$userdata['regcity'] = $this->get_params('regcity');
			$userdata['regsex'] = $this->get_params('regsex');
			$userdata['regqq'] = $this->get_params('regqq');
			$userdata['reghomepage'] = $this->get_params('reghomepage');
			$userdata['regsign'] = $this->get_params('regsign');
			$userdata['regintroduce'] = $this->get_params('regintroduce');
			$userdata['authcode'] = md5(time());
			$userdata['ip'] = '127.0.0.1';
			$userdata['time'] = time();

			if($user_model->insert($userdata)){
				$logindata = array();
				$logindata['regemail'] = $userdata['regemail'];
				$logindata['authcode'] = $userdata['authcode'];
				$logindata['username'] = $userdata['username'];
				$logindata['password'] = $userdata['password'];
				$logindata['end'] = $userdata['end'];
				$url = $this->create_url('user/mail' , $logindata);
				$this->show_message('用户注册成功，请登录',$url,1 );
			}

		}

		$this->display();
	}

	// 推出登录
	public function logoutAction() {
		session_start();
		$_SESSION['login'] = false;
		$_SESSION['userid'] = 1;
		$url = $this->create_url('home/tengxun');
		$this->show_message('用户退出成功',$url,1 );
	}

	//用户邮件验证
	public function mailAction(){
		//get params
		$regemail = $this->get_params('regemail');
		$authcode = $this->get_params('authcode');

		$data = array();
		$data['authcode'] = $authcode;
		$data['end'] = 'end';
		$authurl = $this->create_url('user/authcode' , $data);
		$authurl = $this->get_server_name() . $authurl ;

		// echo $authurl;
		$mailcontent = '<h2>欢迎使用Micheal的毕业设计作品</h2>';
		$mailcontent .= '<h2>这个是集成SNS应用的平台，到我们的平台上你可以更加快速阅读你的微博信息</h2>';
		$mailcontent .= '<h2>只需要点一下以下链接，就可以完成注册过程</h2>';
		$mailcontent .= '<a href = "'.$authurl.'" >你好，这是授权连接，激活你的账号</a>';

		$mailer = $this->module('phpmailer');
		$option = array (
		'host' => 'smtp.qq.com',
		'username' => 'nacheal',
		'password' => 'woaimihong1989',
		'from'=>'nacheal@qq.com',
		'fromname'=>'Micheal',
		'reply'=>'nacheal@qq.com',
		);

		$mailer->set_smtp_config($option);
		$mailer->send_mail('nacheal@qq.com', '这是Micheal的毕业设计注册验证', $mailcontent);

		$url = $this->create_url('sina/index');
		$this->show_message('注册成功，转入首页',$url,1 );

	}

	public function authcodeAction(){
		//get params
		$authcode = $this->get_params('authcode');

		//获取token
		$user_model = $this->model('user');
		$data = $user_model->from('user')->where('authcode=?', $authcode)->select();	

		if(!empty($data)){
			$url = $this->create_url('home/tengxun');
			$this->show_message('验证码成功了，哈哈',$url,1 );
		}
	}

	public function changeuserAction() {

	}

	public function settingAction() {

	}
}